/**
 * 
 */
package sailing.ui.systemsetting;

import java.awt.Window;
import java.sql.Timestamp;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.CashAccount;
import sailing.listener.UI;
import sailing.managers.CashAccountManager;
import sailing.ui.base.CommonJDialog;
import sailing.ui.base.DateChooser;
import sailing.utils.Utils;
import cjlite.logger.Logger;
import cjlite.utils.RandomUtils;
import cjlite.utils.TypeConverter;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class CashAccountSettingDialog extends CommonJDialog {

	private static Logger logger = Logger.thisClass();

	private static String[] btnCaptions = { "确定", "取消" };

	private JTextField openDateTF, balanceTF;

	DateChooser dateChooser;

	private TypeConverter tc = new TypeConverter();

	public CashAccountSettingDialog(Window window, SingleFrameApplication singleFrameApplication) {
		super(window, "现金账户管理", ModalityType.APPLICATION_MODAL, singleFrameApplication);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.base.CommonJDialog#initialContentPanel()
	 */
	@Override
	protected JPanel initialContentPanel() {
		JPanel root = new JPanel();
		root.setBorder(BorderFactory.createEmptyBorder(20, 20, 40, 20));

		FormLayout layout = new FormLayout("right:pref,4dlu,f:100dlu,4dlu,f:50dlu",
				"22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu,2dlu,22dlu");

		root.setLayout(layout);

		CellConstraints cc = new CellConstraints();
		// row 1
		root.add(new JLabel("启用时间："), cc.xy(1, 1));
		openDateTF = new JTextField();
		dateChooser = DateChooser.getInstance("yyyy-MM-dd");
		dateChooser.register(openDateTF);
		root.add(openDateTF, cc.xy(3, 1));
		// row 3
		root.add(new JLabel("初期余额："), cc.xy(1, 3));
		balanceTF = new JTextField();
		root.add(balanceTF, cc.xy(3, 3));
		root.add(new JLabel("(单位：元)"), cc.xy(5, 3));

		this.setSize(430, 300);

		return root;
	}

	@Override
	protected String[] getBottomBtnTxt() {
		return btnCaptions;
	}

	@Override
	protected void performBtnClick(String btnCaption) {
		if (btnCaption.equalsIgnoreCase(btnCaptions[0])) {
			this.performOKAction();
		} else {
			this.performCancelAction();
		}
	}

	private void performCancelAction() {
		this.setVisible(false);
	}

	private void performOKAction() {
		CashAccount ca = new CashAccount();

		String msg = this.validateInput(ca);
		if (msg != null && msg.trim().length() > 0) {
			JOptionPane.showMessageDialog(this, msg, "错误！", JOptionPane.ERROR_MESSAGE);
			return;
		}

		ca.setId(RandomUtils.randomString16());
		ca.setCreateTime(System.currentTimeMillis());
		ca.setModifyTime(System.currentTimeMillis());

		CashAccountManager cam = this.getApplication().getContext().getIoc().getInstance(CashAccountManager.class);
		if (cam.save(ca)) {
			JOptionPane.showMessageDialog(this, "设置成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
			this.setVisible(false);
			UI.refresh();
		} else {
			JOptionPane.showMessageDialog(this, "设置失败！", "错误！", JOptionPane.ERROR_MESSAGE);
		}

	}

	private String validateInput(CashAccount ca) {
		StringBuilder sb = new StringBuilder();

		String value = this.openDateTF.getText();

		if (value == null || value.trim().length() == 0) {
			sb.append("+ 请输入现金账户的启用时间。\n");
		} else {
			try {
				Timestamp ts = Utils.dateParse(value);
				ca.setOpenTime(ts.getTime());
			} catch (Exception e) {
				logger.error("Error on converting String[{0}] to double", e, value);
				sb.append("+ 请输入正确的初始余额\n");
			}
		}

		value = this.balanceTF.getText();

		if (value == null || value.trim().length() == 0) {
			sb.append("+ 请输入正确的初始余额。\n");
		} else {
			try {
				double balance = tc.convert(Double.class, value);
				ca.setInitialBalance(balance);
			} catch (Exception e) {
				logger.error("Error on converting String[{0}] to double", e, value);
				sb.append("+ 请输入正确的初始余额\n");
			}
		}

		return sb.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Dialog#isResizable()
	 */
	@Override
	public boolean isResizable() {
		return false;
	}

}
